package com.sanshi.project.procure.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.log.annotation.SysLog;
import com.sanshi.project.admin.api.dto.ProcureApplyDTO;
import com.sanshi.project.admin.api.entity.ProcureApplyEntity;
import com.sanshi.project.admin.api.entity.ProcureApplyDetailsEntity;
import com.sanshi.project.procure.service.ProcureApplyService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 出入库申请单
 *
 * @author nikon
 * @date 2023-12-10 15:54:35
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/procureApply" )
@Tag(description = "procureApply" , name = "出入库申请单管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ProcureApplyController {

    private final  ProcureApplyService procureApplyService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param procureApply 出入库申请单
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('procure_procureApply_view')" )
    public R getProcureApplyPage(@ParameterObject Page page, @ParameterObject ProcureApplyDTO procureApply) {
        LambdaQueryWrapper<ProcureApplyEntity> wrapper = Wrappers.lambdaQuery();
        // 申请单号
		wrapper.like(StrUtil.isNotBlank(procureApply.getCode()),ProcureApplyEntity::getCode,procureApply.getCode());
        // 出入库类型
		wrapper.in(ArrayUtil.isNotEmpty(procureApply.getInOrOut()),ProcureApplyEntity::getInOrOut,procureApply.getInOrOut());
        // 入库/出库方式
		wrapper.in(ArrayUtil.isNotEmpty(procureApply.getInOrOutMethod()),ProcureApplyEntity::getInOrOutMethod,procureApply.getInOrOutMethod());
        // 申请说明
		wrapper.like(StrUtil.isNotBlank(procureApply.getDescription()),ProcureApplyEntity::getDescription,procureApply.getDescription());
        // 负责人
		wrapper.like(Objects.nonNull(procureApply.getChargePerson()),ProcureApplyEntity::getChargePerson,procureApply.getChargePerson());
        // 状态
		wrapper.in(ArrayUtil.isNotEmpty(procureApply.getStatus()),ProcureApplyEntity::getStatus,procureApply.getStatus());
        return R.ok(procureApplyService.page(page, wrapper));
    }

    /**
     * 通过id查询出入库申请单
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('procure_procureApply_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(procureApplyService.getByIdDeep(id));
    }

    /**
     * 新增出入库申请单
     * @param procureApply 出入库申请单
     * @return R
     */
    @Operation(summary = "新增出入库申请单" , description = "新增出入库申请单" )
    @SysLog("新增出入库申请单" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('procure_procureApply_add')" )
    public R save(@RequestBody ProcureApplyEntity procureApply) {
        return R.ok(procureApplyService.saveDeep(procureApply));
    }

    /**
     * 修改出入库申请单
     * @param procureApply 出入库申请单
     * @return R
     */
    @Operation(summary = "修改出入库申请单" , description = "修改出入库申请单" )
    @SysLog("修改出入库申请单" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('procure_procureApply_edit')" )
    public R updateById(@RequestBody ProcureApplyEntity procureApply) {
        return R.ok(procureApplyService.updateDeep(procureApply));
    }

    /**
     * 通过id删除出入库申请单
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除出入库申请单" , description = "通过id删除出入库申请单" )
    @SysLog("通过id删除出入库申请单" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('procure_procureApply_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(procureApplyService.removeDeep(ids));
    }

    /**
     * 通过id删除出入库申请单子表数据
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除出入库申请单子表数据" , description = "通过id删除出入库申请单子表数据" )
    @SysLog("通过id删除出入库申请单子表数据" )
    @DeleteMapping("/child")
    @PreAuthorize("@pms.hasPermission('procure_procureApply_del')" )
    public R removeChild(@RequestBody Long[] ids) {
        return R.ok(procureApplyService.removeChild(ids));
    }

    /**
     * 导出excel 表格
     * @param procureApply 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('procure_procureApply_export')" )
    public List<ProcureApplyEntity> export(ProcureApplyEntity procureApply,Long[] ids) {
        return procureApplyService.list(Wrappers.lambdaQuery(procureApply).in(ArrayUtil.isNotEmpty(ids), ProcureApplyEntity::getId, ids));
    }
}